<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

namespace app\license\model;

use app\busine\model\LicensePayment;
use think\facade\Db;
use think\Model;

class LicenseCert extends Model
{
    const ZERO = 0;
    const ONE = 1;
    const TWO = 2;
    const THREE = 3;
    const FORE = 4;
    const FIVE = 5;
    const SIX = 6;

    public static $Status = [
        self::ZERO => '待上报',
        self::ONE => '审核中',
        self::TWO => '审核通过',
        self::THREE => '审核拒绝',
        self::FORE => '已撤销',
        self::FIVE => '已中止',
        self::SIX => '已作废',
    ];

    //列表检索
    public function get_list($param = [], $where = [], $whereOr = [])
    {

        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = self::where($where)
            ->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->field('a.*')
            ->alias('a')
            ->order('a.id desc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                $certs = Db::name('license_cert_cates')->whereIn('cert_id', $item->id)->select()->toArray();
                $str = '';
                $cert_types = [];
                foreach ($certs as $k => $v) {
                    if ($str == '') {
                        $str .= $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    } else {
                        $str .= '|' . $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    }
                    $cert_types[] = $v['cert_from_type'];
                }
                $cert_types = implode('|', array_unique($cert_types));
                $item->cert_types = $cert_types;
                $item->projects = $str;
                $item->status_name = self::$Status[(int)$item->check_status];
                if ($item->project_user_id == 0) {
                    $item->project_user_name = '未分配';
                } else {
                    $item->project_user_name = Db::name('admin')->where('id', $item->project_user_id)->value('name');
                }

                if($item->report_date!='0000-00-00'&&$item->gs_date!='0000-00-00'&&!empty($item->report_date)&&!empty($item->gs_date)){
                    $item->safety_valid_date='';
                }
            });
        return $list;
    }


    //列表检索
    public function get_project_list($param = [], $where = [], $whereOr = [])
    {

        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $list = self::where($where)
            ->where(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->field('b.*,a.id as aid,a.project_user_id,a.license_name,a.cert_type,a.cert_project_name,a.cert_valid_date,a.safety_valid_date,a.is_zx,a.file_ids')
            ->alias('a')
            ->leftjoin('license_project b', 'a.id = b.cert_id')
            ->order('a.id desc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                $certs = Db::name('license_cert_cates')->whereIn('cert_id', $item->aid)->select()->toArray();
                $str = '';
                $cert_types = [];
                foreach ($certs as $k => $v) {
                    if ($str == '') {
                        $str .= $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    } else {
                        $str .= '|' . $v['cert_serial'] . '-' . $v['cert_from'] . '-' . $v['cert_level'];
                    }
                    $cert_types[] = $v['cert_from_type'];
                }
                $cert_types = implode('|', array_unique($cert_types));
                $item->cert_types = $cert_types;
                $item->projects = $str;
                $item->status_name = self::$Status[(int)$item->check_status];
                if ($item->project_user_id == 0) {
                    $item->project_user_name = '未分配';
                } else {
                    $item->project_user_name = Db::name('admin')->where('id', $item->project_user_id)->value('name');
                }

                if($item->report_date!='0000-00-00'&&$item->gs_date!='0000-00-00'&&!empty($item->report_date)&&!empty($item->gs_date)){
                    $item->safety_valid_date='';
                }
            });
        return $list;
    }


    // 获取合同详情
    public function detail($id)
    {
        $detail = self::where(['id' => $id])->find();
        if (!empty($detail)) {
            $detail['create_user'] = Db::name('Admin')->where(['id' => $detail['admin_id']])->value('name');
            $detail['status_name'] = self::$Status[(int) $detail['check_status']];
            $share_names = Db::name('Admin')->where([['id', 'in', $detail['share_ids']]])->column('name');
            $detail['share_names'] = implode(',', $share_names);
            if ($detail['is_self']==1) {
               $license =  Db::name('license')->field('file_ids,remark')->where('id',$detail['license_id'])->find();
                $detail['file_ids'] =$license['file_ids'];
                $detail['remark'] =$license['remark'];
            }
            if ($detail['file_ids'] != '') {
                $fileArray = Db::name('File')->where('id', 'in', $detail['file_ids'])->select();
                $detail['fileArray'] = $fileArray;
            }
            $licomes = LicensePayment::where('cert_id',$id)->where('delete_time',0)->order('id asc')->select()->toArray();
            foreach ($licomes as $k => $v){
                $licomes[$k]['status_name'] = self::$Status[(int) $v['check_status']];
            }
            $detail['licomes'] = $licomes;
        }
        return $detail;
    }
}
